package com.cengizc.generator.volcanonames;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Random;
import java.util.Vector;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.TextView;
import android.widget.Toast;
import com.cengizc.generator.volcanonames.R;

public class GenerateNames extends Activity implements Shaker.Callback {
	private Shaker shaker=null;
    /** Called when the activity is first created. */
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        shaker=new Shaker(getApplicationContext(), 1.25d, 500,  this);
        
    }
    
    
    @Override
	public void onDestroy() {
		super.onDestroy();
		
		shaker.close();
	}
    
    @Override
    public boolean onKeyDown(int keycode, KeyEvent event ) {
     if(keycode == KeyEvent.KEYCODE_MENU){
      AlertDialog.Builder dialogBuilder 
      = new AlertDialog.Builder(this)
      .setMessage("Test")
      .setTitle("Menu dialog");
      dialogBuilder.create().show();
     }
     return super.onKeyDown(keycode,event);  
    }
    
    public String GetNewName() {
    	
    	int max_name_length = 30;
		int preferable_num_of_words = 3;
		int num_of_generated = 1;
		
		Vector<String> vc=new Vector<String>();
			    
	    InputStream ins = getResources().openRawResource(R.raw.words);

	    BufferedReader d = new BufferedReader(new InputStreamReader(ins));
	    
	    String line = null;
	    try {
			while (( line = d.readLine()) != null){
			      vc.add(line);
			    }
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    
	    String word = null;
	    
	    int num_of_available_words = 567;
	    int randomIndex;
	    
	    Random generator = new Random();
	    
	    for (int i=0; i<num_of_generated; i++)
	    {
	    	word = "";
	    	for (int j=0; j<preferable_num_of_words; j++)
	    	{
	    		randomIndex = generator.nextInt( num_of_available_words );
	    		word = word + vc.get(randomIndex);
	    	}
	    	word = word + "jökull";
	    }
    	return word;
    }
	
	public void shakingStarted() {
		Toast.makeText(getApplicationContext(), "Sallanıyoruz Rabbülaleminnn", Toast.LENGTH_SHORT).show();
		
		TextView text = (TextView)findViewById(R.id.txtName);
		text.setText(GetNewName());
	}
	
	public void shakingStopped() {
		/*TextView text = (TextView)findViewById(R.id.txtName);
		text.setText("Durduk"); */
	}
    
    
}

